﻿$().ready(function () {
    var askMaxLength = 500;
    var seriesCourseID = $("#seriesCourseID").val();
    var sectionID = $("#sectionID").val();
    function getQuestions() {
        $.post("/course/questions", { seriesCourseID: seriesCourseID, sectionID: sectionID, filter: "all", viewName: "QuestionInPlayer" }, function (data) {
            $("#play-questions").html(data);
            $("#play-questions").scrollTop(0);
            $(".question-answers").hide();
            $(".question-answer-expender").click(function () {
                var id = $(this).attr("target");
                var clickedTarget = $("#" + id);
                var isTargetShow = false;
                if (clickedTarget.css("display") == "block") {
                    isTargetShow = true;
                }
                $(".question-answers").hide();
                if (!isTargetShow) {
                    clickedTarget.show();
                }
            });
            $(".unfolder-button").click(function () {
                var qid = $(this).attr("targetID");
                var folderItemID = "#question-folder-" + qid;
                var unfolderItemID = "#question-unfolder-" + qid;
                $(folderItemID).hide();
                $(unfolderItemID).show();
            });
            $(".folder-button").click(function () {
                var qid = $(this).attr("targetID");
                var folderItemID = "#question-folder-" + qid;
                var unfolderItemID = "#question-unfolder-" + qid;
                $(folderItemID).show();
                $(unfolderItemID).hide();
            });
        })
    }
    getQuestions();

    $("#play-ask-submit-button").click(function () {
        if ($("#play-ask-textarea").val().length <= 0) {
            $("#play-message").css("visibility", "visible");
            return;
        }

        $.post("/seriescourses/playaskquestion", { contents: encodeURIComponent($("#play-ask-textarea").val()), seriseCourseID: seriesCourseID, sectionID: sectionID }, function (data) {
            var res = JSON.parse(data);
            if (res.Status == "success" && res.Message == "ok") {
                $("#play-cover").css("visibility", "hidden");
                $("#play-message").css("visibility", "hidden");
                $("#play-question-ask").css("background", "rgb(61,66,66)");
                $("#play-ask-textarea").val("")
                $("#play-ask-text-number").html("剩余字数500字");
                getQuestions();
            }
            else {

            }
        });
    });




    //点击讲师答疑

    $("#play-question-tag").click(function () {
        $("#play-question-tag")[0].className = "play-question-tag-select";
        $("#play-outline-tag")[0].className = "play-outline-tag";
        $("#play-onte-tag")[0].className = "play-question-tag";

        $("#play-onte-content").css("visibility", "hidden");
        $("#play-outline-content").css("visibility", "hidden");
        $("#play-question-content").css("visibility", "visible");
    });



    //点击课程列表
    $("#play-outline-tag").click(function () {
        $("#play-question-tag")[0].className = "play-question-tag";
        $("#play-onte-tag")[0].className = "play-question-tag";
        $("#play-outline-tag")[0].className = "play-outline-tag-select";

        $("#play-outline-content").css("visibility", "visible");
        $("#play-question-content").css("visibility", "hidden");
        $("#play-onte-content").css("visibility", "hidden");
    });
    //自己写点击笔记列表
    $("#play-onte-tag").click(function () {
        $("#play-question-tag")[0].className = "play-question-tag";
        $("#play-outline-tag")[0].className = "play-outline-tag";
        $("#play-onte-tag")[0].className = "play-outline-tag-select";
        $("#play-onte-content").css("visibility", "visible");
        $("#play-question-content").css("visibility", "hidden");
        $("#play-outline-content").css("visibility", "hidden");
    });


    $("#play-question-ask").click(function () {
        var accountIDEle = document.getElementById("accountID");
        if (accountIDEle == null) {
            window.location.href = "/signin";
        }
        else {
            $("#play-question-ask").css("background", "rgb(49,53,53)");
            $("#play-cover").css("visibility", "visible");
        }
    });

    $("#play-cover").click(function () {
        $("#play-cover").css("visibility", "hidden");
        $("#play-message").css("visibility", "hidden");
        $("#play-question-ask").css("background", "rgb(61,66,66)");
    });


    $("#play-ask-content").click(function () {
        return false;
    });

    $("#play-ask-textarea").keyup(function () {
        var length = $("#play-ask-textarea").val().length;
        //var re = /\n/g;
        //length += $("#play-ask-textarea").val().match(re) == null ? 0 : $("#play-ask-textarea").val().match(re).length;
        //if (length > askMaxLength)
        //    length = askMaxLength;
        $("#play-message").css("visibility", "hidden");
        $("#play-ask-text-number").html("剩余字数" + (askMaxLength - length) + "字");
    });

    $("#play-ask-textarea").keydown(function (e) {
        if (e.ctrlKey && parseInt(e.keyCode) == 66)
            return false;

        if (e.ctrlKey && parseInt(e.keyCode) == 65)
            return true;

        if (e.ctrlKey && parseInt(e.keyCode) == 67)
            return true;

        if (e.ctrlKey && parseInt(e.keyCode) == 86)
            return true;

        var code = parseInt(e.keyCode);
        if (code == 8 || code == 46)
            return true;


        var length = $("#play-ask-textarea").val().length;
        if (length >= askMaxLength)
            return false;
        else
            return true;
    });


    //显示隐藏侧边栏
    var isSideOpen = true;
    $("#play-slidebar-toggle").click(function () {
        if (isSideOpen == true) {
            $("#play-content-right").animate({ width: "0" });
            $("#play-slidebar-toggle").animate({ right: "0" });
            $("#play-content-left").animate({ right: "30px" });
            $(this).toggleClass("to-right");
            $(this).toggleClass("to-left");
            isSideOpen = false;
        }else if (isSideOpen == false) {
            $("#play-content-left").animate({ right: "350px" });
            $("#play-slidebar-toggle").animate({ right: "320px" });
            $("#play-content-right").animate({ width: "320px" });
            $(this).toggleClass("to-left");
            $(this).toggleClass("to-right");
            isSideOpen = true;
        }

    });

});